﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS.SAC.Conditions
{
    #region Using Statements

    using System;

    #endregion

    public class PseudoFactory
    {
        public static Condition CreatePseudoCondition(string funcName, string funcArg)
        {
            switch (funcName.ToLowerInvariant())
            {
                case "checked":
                    return new PseudoChecked();

                case "disabled":
                    return new PseudoDisabled();

                case "empty":
                    return new PseudoEmpty();

                case "enabled":
                    return new PseudoEnabled();

                case "nth-child":
                    return new PseudoNthChild(funcArg);

                case "nth-last-child":
                case "nth-of-type":
                case "nth-last-of-child":
                case "first-of-type":
                case "last-of-type":
                case "first-child":
                case "last-child":
                case "only-child":
                case "only-of-type":
                    throw new NotImplementedException();

                case "root":
                    return new PseudoRoot();

                default:
                    throw new ArgumentException("Invalid pseudo function");
            }
        }

        public static Condition CreatePseudoCondition(string spec)
        {
            try
            {
                string arg = "";

                if (spec.IndexOf('(') > 0)
                {
                    arg = spec.Substring(spec.IndexOf('(') + 1,
                        spec.LastIndexOf(')') - spec.IndexOf('(') - 1);
                    spec = spec.Substring(0, spec.IndexOf('('));
                }
                return CreatePseudoCondition(spec.Substring(1), arg);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Could not create pseudo condition for {0}: {1}",
                    spec, ex.Message);
            }
            return null;
        }
    }
}
